package com.ciwong.epaper.modules.dictation.adpter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.dictation.bean.DictationAnswerInfo;
import com.ciwong.epaper.modules.me.adapter.BaseCommonAdapter;

import java.util.List;

/**
 * Created by keepon on 2018/7/13.
 */
public class DictationRedoAdapter extends BaseCommonAdapter<DictationAnswerInfo> {

    private Context mContext;
    /**
     * 默认服务包剩余使用时间
     */
    public boolean serviceExpired;

    public DictationRedoAdapter(Context context, List<DictationAnswerInfo> dataList, boolean serviceExpired) {
        super(dataList);
        this.mContext = context;
        this.serviceExpired = serviceExpired;
    }

    public DictationRedoAdapter(List<DictationAnswerInfo> dataList) {
        super(dataList);
    }

    @Override
    public int getItemResId(int position) {
        return R.layout.item_dictation_redo;
    }

    @Override
    public DictationRedoAdapter.ViewHolder createViewHolder(View convertView, int position) {
        DictationRedoAdapter.ViewHolder viewHolder = new DictationRedoAdapter.ViewHolder();
        viewHolder.mTvWord = convertView.findViewById(R.id.tv_word);
        viewHolder.mIvIcon = convertView.findViewById(R.id.iv_icon);
        viewHolder.mLine = convertView.findViewById(R.id.line);
        viewHolder.mTvWordPretations = convertView.findViewById(R.id.tv_word_pretations);
        viewHolder.mRelativeLayout = convertView.findViewById(R.id.rl_container);
        return viewHolder;
    }

    @Override
    public void showData(final DictationAnswerInfo listenWordAnswerInfo, Object holder, final int position) {
        if (holder instanceof DictationRedoAdapter.ViewHolder) {
            DictationRedoAdapter.ViewHolder viewHolder = (ViewHolder) holder;
            int curentIndex = position + 1;
            String wordAnswer = listenWordAnswerInfo.getWordAnswer();
            String pretations = listenWordAnswerInfo.getPretations();
            viewHolder.mTvWord.setText(curentIndex + "." + (!TextUtils.isEmpty(wordAnswer) ? wordAnswer : ""));
            viewHolder.mTvWordPretations.setText((!TextUtils.isEmpty(pretations) ? pretations : ""));
            if (listenWordAnswerInfo != null) {
                if (serviceExpired) {
                    if (listenWordAnswerInfo.isRight()) {
                        viewHolder.mIvIcon.setImageResource(R.mipmap.icon_answer_right);
                        if (mContext != null) {
                            viewHolder.mTvWord.setTextColor(mContext.getResources().getColor(R.color.color_light_green));
                        }
                    } else {
                        if (mContext != null) {
                            viewHolder.mTvWord.setTextColor(mContext.getResources().getColor(R.color.light_red));
                        }
                        viewHolder.mIvIcon.setImageResource(R.mipmap.icon_answer_wrong);
                    }
                } else {
                    viewHolder.mIvIcon.setVisibility(View.GONE);
                    viewHolder.mTvWord.setTextColor(mContext.getResources().getColor(R.color.black));
                }
                viewHolder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnRedoWordListener != null && !listenWordAnswerInfo.isRight()) {
                            mOnRedoWordListener.redoWord(position);
                        }
                    }
                });
            }
            if (position == getCount() - 1) {
                viewHolder.mLine.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.mLine.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setOnRedoWordListener(OnRedoWordListener reReadWordListener) {
        this.mOnRedoWordListener = reReadWordListener;
    }

    OnRedoWordListener mOnRedoWordListener;

    public interface OnRedoWordListener {
        void redoWord(int position);
    }

    public static class ViewHolder {
        RelativeLayout mRelativeLayout;
        TextView mTvWord;
        ImageView mIvIcon;
        ImageView mLine;
        TextView mTvWordPretations;
    }
}
